package com.sany.taskmanager.http;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.sany.taskmanager.global.Key;
import com.sany.taskmanager.util.PreferenceUtil;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 请求头Token拦截器
 */
public class TokenHeaderInterceptor implements Interceptor {
    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        String token = PreferenceUtil.getPreferences().getString(Key.SharedKey.TOKEN);
        Request originalRequest = chain.request();
        if (TextUtils.isEmpty(token)) {
            return chain.proceed(originalRequest);
        }
        Request newRequest = originalRequest.newBuilder().header("token", token).build();
        return chain.proceed(newRequest);
    }
}
